/*
Copyright 2010 Sieric
http://sieric.ca

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vanilla;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PipedInputStream;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;

import savior.fs.FileSystem;
import savior.messaging.Logger;
import savior.messaging.Manager;

@SuppressWarnings("serial")
public class Backup extends JComponent implements ActionListener, Logger {
	private Viewer parentViewer;
	private JTextArea info;
	private JTextArea actions;
	private JTextArea errors;
	PipedInputStream pipeIn;
	Backup_Thread myBackup; 
	private JButton bStop, bPause;
	
	public Backup(String _profile, Viewer _v) {
		JLabel tmplbl;
		JSeparator tmpsep;
		JScrollPane tmpscr;
		JButton tmpbut;
		
		int y = 0;
		int width = 800;
		
		parentViewer = _v;
		
		// Show the title
		JLabel title = new JLabel("Backup");
		title.setFont(title.getFont().deriveFont(Font.BOLD, 16));
		title.setBounds(1, y, width, 20);
		y += 30;
		add(title);
		
		// Show the Info separator
		tmpsep = new JSeparator();
		tmpsep.setBounds(1, y, width, 5);
		y += 5;
		add(tmpsep);
		
		tmplbl = new JLabel("Informations");
		tmplbl.setFont(tmplbl.getFont().deriveFont(Font.BOLD, 14));
		tmplbl.setBounds(1, y, width, 15);
		y += 20;
		add(tmplbl);
		
		// Show info
		info = new JTextArea();
		info.setEditable(false);
		tmpscr = new JScrollPane(info);
		tmpscr.setBounds(1, y, 785, 200);
		y += 200;
		add(tmpscr);
		
		// Show the Actions separator
		tmpsep = new JSeparator();
		tmpsep.setBounds(1, y, width, 5);
		y += 5;
		add(tmpsep);
		
		tmplbl = new JLabel("Actions");
		tmplbl.setFont(tmplbl.getFont().deriveFont(Font.BOLD, 14));
		tmplbl.setBounds(1, y, width, 15);
		y += 20;
		add(tmplbl);
		
		// Show Actions
		actions = new JTextArea();
		actions.setEditable(false);
		tmpscr = new JScrollPane(actions);
		tmpscr.setBounds(1, y, 785, 200);
		y += 200;
		add(tmpscr);
		
		// Show the Errors separator
		tmpsep = new JSeparator();
		tmpsep.setBounds(1, y, width, 5);
		y += 5;
		add(tmpsep);
		
		tmplbl = new JLabel("Errors");
		tmplbl.setFont(tmplbl.getFont().deriveFont(Font.BOLD, 14));
		tmplbl.setBounds(1, y, width, 15);
		y += 20;
		add(tmplbl);
		
		// Show Errors
		errors = new JTextArea();
		errors.setEditable(false);
		tmpscr = new JScrollPane(errors);
		tmpscr.setBounds(1, y, 785, 200);
		y += 200;
		add(tmpscr);	
		
		// Show the buttons
		tmpbut = new JButton("Cancel");
		tmpbut.addActionListener(this);
		tmpbut.setBounds(1, y, 80, 20);
		add(tmpbut);
		
		bStop = new JButton("Stop");
		bStop.addActionListener(this);
		bStop.setBounds(width/2-40, y, 80, 20);
		add(bStop);
		
		bPause = new JButton("Pause/Resume");
		bPause.addActionListener(this);
		bPause.setBounds(width-150, y, 130, 20);
		add(bPause);
		
		y += 20;

		parentViewer.AnimateChangeComponent(this, width, y+50);
		
		// Add as a logger
		Manager.getInstance().replaceLoggers(this);
		
		// Start the backup
		FileSystem source = null;
		FileSystem destination = null;
		List<String> ignoreList = null;
		if (Profile.ParseProfile(_profile)) {
			// Get the parameters
			source = Profile.getSource();
			destination = Profile.getDestination();
			ignoreList = Profile.getIgnoreList();
			
			// Start the backup
			myBackup = new Backup_Thread(source, destination, ignoreList);
			myBackup.start();
		} else {
			Manager.getInstance().error("The profile is not valid");
		}
	}

	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent arg0) {
		// Cancel button
		if (arg0.getActionCommand().compareTo("Cancel") == 0) {
			if (myBackup != null) myBackup.stop();
			parentViewer.ToStart();
		}
		
		// Stop button
		if (arg0.getActionCommand().compareTo("Stop") == 0) {
			if (myBackup != null) myBackup.stop();
			// Disable pause and stop buttons
			bPause.setEnabled(false);
			bStop.setEnabled(false);
		}
		
		// Pause button
		if (arg0.getActionCommand().compareTo("Pause/Resume") == 0) {
			myBackup.TogglePause();
		}
	}

	@Override
	public void action(String text) {
		actions.setText(text + "\n" + actions.getText());
	}
	
	@Override
	public void debug(String text) {
		System.out.println(text);
	}

	@Override
	public void error(String text) {
		errors.setText(text + "\n" + errors.getText());
	}

	@Override
	public void information(String text) {
		info.setText(text + "\n" + info.getText());
	}
}
